SlideShare a Scribd company logo
Class No.26  Data Structures http://ecomputernotes.com
Heap code in C++ template <class eType> void Heap<eType>::deleteMin( eType & minItem ) { if( isEmpty( ) ) { cout <<  &quot; heap is empty. “ << endl ; return; } minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); } http://ecomputernotes.com
Heap code in C++ //  hole is the index at which the percolate begins. template <class eType> void Heap<eType>::percolateDown( int hole ) { int child; eType tmp = array[ hole ]; for( ; hole * 2 <= currentSize; hole = child ) { child = hole * 2; if( child != currentSize &&  array[child+1] < array[ child ] ) child++;  //  right child is smaller if( array[ child ] < tmp ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; } http://ecomputernotes.com
Heap code in C++ template <class eType> const eType& Heap<eType>::getMin( ) { if( !isEmpty( ) ) return array[ 1 ]; } template <class eType> void Heap<eType>::buildHeap(eType* anArray, int n ) { for(int i = 1; i <= n; i++)  array[i] = anArray[i-1]; currentSize = n;  for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i ); } http://ecomputernotes.com
Heap code in C++ template <class eType> bool Heap<eType>::isEmpty( ) { return currentSize == 0; } template <class eType> bool Heap<eType>::isFull( ) { return currentSize == capacity; } template <class eType> int Heap<eType>::getSize( ) { return currentSize; } http://ecomputernotes.com
BuildHeap in Linear Time How is buildHeap a linear time algorithm? I.e., better than Nlog 2 N? We need to show that the sum of heights is a linear function of  N  (number of nodes). Theorem :  For a perfect binary tree of height  h  containing 2 h  + 1  –  1  nodes, the sum of the heights of nodes is 2 h  + 1  –  1  – ( h +1 ), or  N - h-1. http://ecomputernotes.com
BuildHeap in Linear Time It is easy to see that this tree consists of (2 0 ) node at height  h , 2 1  nodes at height  h  –1, 2 2  at  h- 2   and, in general, 2 i  nodes at  h – i. http://ecomputernotes.com
Complete Binary Tree A B h  : 2 0  nodes H D I E J K C L F M G N O h  -1: 2 1  nodes h  -2: 2 2  nodes h  -3: 2 3  nodes http://ecomputernotes.com
BuildHeap in Linear Time The sum of the heights of all the nodes is then S   =    2 i ( h – i ), for  i  = 0 to  h-1 =  h + 2(h-1) + 4(h-2) + 8(h-3)+ ….. + 2 h-1  (1) Multiplying by 2 gives the equation 2S = 2 h + 4(h-1) + 8(h-2) + 16(h-3)+ ….. + 2 h  (2) Subtract the two equations to get S = - h + 2 + 4 + 8 + 16+ ….. + 2 h-1  +2 h = (2 h+1  – 1) - (h+1) Which proves the theorem. http://ecomputernotes.com
BuildHeap in Linear Time Since a complete binary tree has between  2 h  and   2 h+1  nodes S  = (2 h+1  – 1) - (h+1)   N  -  log 2 (N+1)  Clearly, as  N  gets larger, the log 2 ( N  +1) term becomes insignificant and S becomes a function of  N . http://ecomputernotes.com
BuildHeap in Linear Time Another way to prove the theorem. The  height  of a node in the tree = the number of edges on the longest downward path to a leaf  The height of a tree = the height of its root For any node in the tree that has some height  h , darken h tree edges Go down tree by traversing left edge then only right edges There are  N  – 1 tree edges, and  h  edges on right path, so number of darkened edges is  N  – 1 –  h , which proves the theorem. http://ecomputernotes.com
Height 1 Nodes Marking the left edges for height 1 nodes http://ecomputernotes.com
Height 2 Nodes Marking the first left edge and the subsequent right edge for height 2 nodes http://ecomputernotes.com
Height 3 Nodes Marking the first left edge and the subsequent two right edges for height 3 nodes http://ecomputernotes.com
Height 4 Nodes Marking the first left edge and the subsequent three right edges for height 4 nodes http://ecomputernotes.com
Theorem N=31, treeEdges=30, H=4, dottedEdges=4 (H). Darkened Edges = 26 = N-H-1 (31-4-1) http://ecomputernotes.com

More Related Content

PPTX
Blind os
PPTX
C# 8 and Beyond
PDF
Functional Programming for Fun and Profit
DOCX
Practical no 4
PPTX
Introduction to Algorithm
PPTX
Asymptotic Notation
PDF
Algorithms
PDF
7. Pointer Arithmetic
Blind os
C# 8 and Beyond
Functional Programming for Fun and Profit
Practical no 4
Introduction to Algorithm
Asymptotic Notation
Algorithms
7. Pointer Arithmetic

What's hot (19)

PDF
CS 210 Project UML
PPTX
IGraph a tool to analyze your network
PPT
35th 36th Lecture
PPTX
Rational number revision
PPTX
Mini project title prime number generator
DOCX
Public class arithmetic operatordemo
PPTX
Derivadas
PPT
07f03 carryskip
PPTX
Space complexity
PDF
Analysis of different bit carry look ahead adder using verilog code 2
PPTX
Ripple look-ahead-header
PPT
carry look ahead adder
PPT
Thesis PPT
PDF
Linked list Output tracing
PDF
C mcq practice test 2
DOCX
Lisp programming
PPTX
Hello world program
PDF
Introduction to cython: example of GCoptimization
DOCX
Practical no 3
CS 210 Project UML
IGraph a tool to analyze your network
35th 36th Lecture
Rational number revision
Mini project title prime number generator
Public class arithmetic operatordemo
Derivadas
07f03 carryskip
Space complexity
Analysis of different bit carry look ahead adder using verilog code 2
Ripple look-ahead-header
carry look ahead adder
Thesis PPT
Linked list Output tracing
C mcq practice test 2
Lisp programming
Hello world program
Introduction to cython: example of GCoptimization
Practical no 3
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 25
PDF
computer notes - Deleting a node
PPT
computer notes - Data Structures - 23
PPT
computer notes - Data Structures - 24
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 34
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 7
computer notes - Data Structures - 18
computer notes - Data Structures - 2
computer notes - Data Structures - 25
computer notes - Deleting a node
computer notes - Data Structures - 23
computer notes - Data Structures - 24
computer notes - Data Structures - 16
computer notes - Data Structures - 31
computer notes - Data Structures - 34
computer notes - Data Structures - 5
computer notes - Data Structures - 32
computer notes - Data Structures - 6
computer notes - Data Structures - 21
computer notes - Data Structures - 29
computer notes - Data Structures - 22
computer notes - Data Structures - 12
computer notes - Data Structures - 14
computer notes - Data Structures - 20
computer notes - Data Structures - 4
computer notes - Data Structures - 7
Ad

Similar to computer notes - Data Structures - 26 (20)

PDF
Heaps
PPT
thisisheapsortpptfilewhichyoucanuseanywhereanytim
PPT
Computer notes - Build Heap
PPT
lecture 5
PPT
Heapsort 1
PPTX
Heapsort
PPT
Heapsortokkay
PDF
CS-102 Course_ Binary Tree Lectures .pdf
PDF
Algorithm chapter 6
PPT
Complete binary tree and heap
PPTX
05 heap 20161110_jintaeks
PDF
Heap Hand note
PDF
LEC 8-DS ALGO(heaps).pdf
PPTX
Lecture 3 - Data Structure File Organization
PPTX
Tree traversal techniques
PPTX
Data Structures and Agorithm: DS 18 Heap.pptx
PPTX
Lecture 11.1 : heaps
PPT
3-heapsort gajajag jahajahab jabajanan jabajan
Heaps
thisisheapsortpptfilewhichyoucanuseanywhereanytim
Computer notes - Build Heap
lecture 5
Heapsort 1
Heapsort
Heapsortokkay
CS-102 Course_ Binary Tree Lectures .pdf
Algorithm chapter 6
Complete binary tree and heap
05 heap 20161110_jintaeks
Heap Hand note
LEC 8-DS ALGO(heaps).pdf
Lecture 3 - Data Structure File Organization
Tree traversal techniques
Data Structures and Agorithm: DS 18 Heap.pptx
Lecture 11.1 : heaps
3-heapsort gajajag jahajahab jabajanan jabajan

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
computer notes - Data Structures - 30
computer notes - Data Structures - 39
computer notes - Data Structures - 11
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 35
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 10
Computer notes - Controlling User Access

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
Teaching material agriculture food technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
MYSQL Presentation for SQL database connectivity
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
Digital-Transformation-Roadmap-for-Companies.pptx
The AUB Centre for AI in Media Proposal.docx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Teaching material agriculture food technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

computer notes - Data Structures - 26

  • 1. Class No.26 Data Structures http://ecomputernotes.com
  • 2. Heap code in C++ template <class eType> void Heap<eType>::deleteMin( eType & minItem ) { if( isEmpty( ) ) { cout << &quot; heap is empty. “ << endl ; return; } minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); } http://ecomputernotes.com
  • 3. Heap code in C++ // hole is the index at which the percolate begins. template <class eType> void Heap<eType>::percolateDown( int hole ) { int child; eType tmp = array[ hole ]; for( ; hole * 2 <= currentSize; hole = child ) { child = hole * 2; if( child != currentSize && array[child+1] < array[ child ] ) child++; // right child is smaller if( array[ child ] < tmp ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; } http://ecomputernotes.com
  • 4. Heap code in C++ template <class eType> const eType& Heap<eType>::getMin( ) { if( !isEmpty( ) ) return array[ 1 ]; } template <class eType> void Heap<eType>::buildHeap(eType* anArray, int n ) { for(int i = 1; i <= n; i++) array[i] = anArray[i-1]; currentSize = n; for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i ); } http://ecomputernotes.com
  • 5. Heap code in C++ template <class eType> bool Heap<eType>::isEmpty( ) { return currentSize == 0; } template <class eType> bool Heap<eType>::isFull( ) { return currentSize == capacity; } template <class eType> int Heap<eType>::getSize( ) { return currentSize; } http://ecomputernotes.com
  • 6. BuildHeap in Linear Time How is buildHeap a linear time algorithm? I.e., better than Nlog 2 N? We need to show that the sum of heights is a linear function of N (number of nodes). Theorem : For a perfect binary tree of height h containing 2 h + 1 – 1 nodes, the sum of the heights of nodes is 2 h + 1 – 1 – ( h +1 ), or N - h-1. http://ecomputernotes.com
  • 7. BuildHeap in Linear Time It is easy to see that this tree consists of (2 0 ) node at height h , 2 1 nodes at height h –1, 2 2 at h- 2 and, in general, 2 i nodes at h – i. http://ecomputernotes.com
  • 8. Complete Binary Tree A B h : 2 0 nodes H D I E J K C L F M G N O h -1: 2 1 nodes h -2: 2 2 nodes h -3: 2 3 nodes http://ecomputernotes.com
  • 9. BuildHeap in Linear Time The sum of the heights of all the nodes is then S =  2 i ( h – i ), for i = 0 to h-1 = h + 2(h-1) + 4(h-2) + 8(h-3)+ ….. + 2 h-1 (1) Multiplying by 2 gives the equation 2S = 2 h + 4(h-1) + 8(h-2) + 16(h-3)+ ….. + 2 h (2) Subtract the two equations to get S = - h + 2 + 4 + 8 + 16+ ….. + 2 h-1 +2 h = (2 h+1 – 1) - (h+1) Which proves the theorem. http://ecomputernotes.com
  • 10. BuildHeap in Linear Time Since a complete binary tree has between 2 h and 2 h+1 nodes S = (2 h+1 – 1) - (h+1)  N - log 2 (N+1) Clearly, as N gets larger, the log 2 ( N +1) term becomes insignificant and S becomes a function of N . http://ecomputernotes.com
  • 11. BuildHeap in Linear Time Another way to prove the theorem. The height of a node in the tree = the number of edges on the longest downward path to a leaf The height of a tree = the height of its root For any node in the tree that has some height h , darken h tree edges Go down tree by traversing left edge then only right edges There are N – 1 tree edges, and h edges on right path, so number of darkened edges is N – 1 – h , which proves the theorem. http://ecomputernotes.com
  • 12. Height 1 Nodes Marking the left edges for height 1 nodes http://ecomputernotes.com
  • 13. Height 2 Nodes Marking the first left edge and the subsequent right edge for height 2 nodes http://ecomputernotes.com
  • 14. Height 3 Nodes Marking the first left edge and the subsequent two right edges for height 3 nodes http://ecomputernotes.com
  • 15. Height 4 Nodes Marking the first left edge and the subsequent three right edges for height 4 nodes http://ecomputernotes.com
  • 16. Theorem N=31, treeEdges=30, H=4, dottedEdges=4 (H). Darkened Edges = 26 = N-H-1 (31-4-1) http://ecomputernotes.com

Editor's Notes

  • #3: End of lecture 31, start of 32
  • #17: End of lecture 32